package com.lbs.service.Impl;

import com.lbs.dao.CourseDao;
import com.lbs.dao.Impl.CourseDaoImpl;
import com.lbs.entity.Course;
import com.lbs.service.CourseService;
import com.lbs.utils.PageUtils;

import java.util.List;

/**
 * @author Administrator
 * @date 2021/6/1 11:51
 * @description
 **/
public class CourseServiceImpl implements CourseService {

    CourseDao courseDao = new CourseDaoImpl();

    /**
     * 添加课程信息
     *
     * @param course
     * @return
     */
    @Override
    public boolean addCourse(Course course) {
        return courseDao.insertCourse(course) > 0;
    }

    /**
     * 分页查询课程信息
     *
     * @param search
     * @param pageSize
     * @param currentPage
     * @return
     */
    @Override
    public PageUtils<Course> pageCourse(String search, int pageSize, int currentPage) {
        StringBuilder builder = new StringBuilder();
        String sql = "select * from course where courseName like ?";
        builder.append(sql);
        int total = courseDao.findCourseByName(builder.toString(), "%" + search + "%").size();
        builder.append(" limit ?,?");
        List<Course> courses = courseDao.findCourseByName(builder.toString(), "%" + search + "%", (currentPage - 1) * pageSize, pageSize);
        PageUtils<Course> pageUntils = new PageUtils<>(pageSize, currentPage, total, courses);
        return pageUntils;
    }

    /**
     * 修改课程信息
     * @param course
     * @return
     */
    @Override
    public boolean updateCourse(Course course) {
        return courseDao.updateCourseById(course)>0;
    }

    /**
     * 批量删除课程信息
     * @param cids
     * @return
     */
    @Override
    public boolean deleteCourse(String[] cids) {
        if (cids!=null&&cids.length>0){
            for (String cid : cids) {
                courseDao.deleteCourseById(cid);
            }
            return true;
        }else {
            return false;
        }
    }

    /**
     * 查询所有的课程信息
     * @return
     */
    @Override
    public List<Course> selectCourseAll() {
        return courseDao.selectAll();
    }


}
